package com.qinghe.pro.controller.apis;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.apis.Tmall;
import com.qinghe.pro.entity.main.User;
import com.qinghe.pro.service.apis.TmallService;
import com.qinghe.pro.service.main.UserService;
import com.qinghe.pro.util.log.Log;
import com.qinghe.pro.util.log.LogMessageObject;
import com.qinghe.pro.util.log.impl.LogUitls;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;
import com.qinghe.utils.Utils;

@Controller
@RequestMapping("/api/tmall")
public class TmallController {

	@Autowired
	@Qualifier("tmallServiceImpl")
	private TmallService tmallService;

	@Autowired
	@Qualifier("userServiceImpl")
	private UserService userService;

	/**
	 * 天猫商品评价接口
	 * 
	 * @param username
	 * @param password
	 * @param keyword
	 * 
	 * @return json
	 * @author longfei
	 */
	@Log(type = "API记录", message = "{0}")
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public @ResponseBody AjaxObject create(String username, String password, String keyword) {
		if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(keyword)) {
			return AjaxObject.newError("参数不合法！");
		}
		User user = userService.findByUsername(username);
		if (user == null) {
			return AjaxObject.newError("用户不存在！");
		} else if (user.getStatus() == User.STATUS_DISABLED) {
			return AjaxObject.newError("用户已禁用！");
		} else if (!DigestUtils.md5Hex(password).endsWith(user.getPassword())) {
			return AjaxObject.newError("密码错误！");
		}
		
		Tmall tmall = new Tmall();
		tmall.setTitle(keyword);
		
		Page page = new Page();
		page.setNumPerPage(100);
		List<Tmall> list = tmallService.findByPage(tmall, page);
		if (list.size() == 0) {
			return AjaxObject.newError("没有查询到数据结果！");
		}

		LogUitls.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{"调用了天猫商品评价接口查询到数据。"}));
		return AjaxObject.newData(Utils.JsonUtil.toJson(list));
	}

}
